package com.wa.demo;

import com.wa.annotation.AllchipsLog;
import com.wa.annotation.AllchipsLogModel;
import com.wa.enums.LogModuleEnum;
import com.wa.enums.LogTypeEnum;
import com.wa.enums.OperationLogTemplateEnum;
import com.wa.page.BaseLogEntity;
import com.wa.dto.OperationLogInfoDto;
import com.wa.dto.OperatorInfoDto;
import com.wa.pojo.User;
import com.wa.util.CommUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gary
 * @date 2021年11月25日 11:50
 */
public class LogService {

    private Logger logger = LoggerFactory.getLogger(LogService.class);

    /**
     * 测试方法（变更日志+操作日志同时）
     *
     * @Author Dawn
     * @Date 2021/10/15 11:33
     */
    @AllchipsLog(module = LogModuleEnum.ACCOUNT, type = {LogTypeEnum.C_ADD, LogTypeEnum.O_LOGIN})
//    public void test(@AllchipsLogModel UserMessagePojo userMessagePojo) throws BackendException {
//        userMessagePojo.setOperationLogInfo(new OperationLogInfoDto());
//        userMessagePojo.getOperationLogInfo().setEntityId(1L);
//        userMessagePojo.getOperationLogInfo().setEntityName("用户1");
//        userMessagePojo.getOperationLogInfo().setOperationLogTemplate(OperationLogTemplateEnum.LOGIN);
//        Map<String, Object> map = new HashMap<>();
//        map.put("userId", 1);
//        map.put("userLoginName", "jack111");
//        map.put("employeeName", "jack");
//        map.put("result", "成功");
//        userMessagePojo.getOperationLogInfo().setDescParamMap(map);
//    }

//    /**
//     * @Description: 根据表查询日历
//     * @Param:
//     * @return:
//     * @Author: james.yang
//     * @Date: 2021/10/21
//     */
//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public List<BasicAllchipsLog> selectByPage(BasicAllchipsLog record, Page<BasicAllchipsLog> page) throws Exception {
//        if (record == null) {
//            throw new BackendException("参数不能为空");
//        }
//        // 获取日志种类
//        if (null == record.getType() && null == record.getLogCategory()) {
//            throw new BackendException("日志类型为空");
//        }
//        Integer categoryCode = record.getLogCategory();
//        Integer platform = record.getPlatform();
//        if (null == record.getPlatform()) {
//            platform = LogPlatformEnum.NONE.getStatusCode();
//        }
//        if (null == categoryCode) {
//            LogTypeEnum logTypeEnum = LogTypeEnum.get(record.getType());
//            if (CommUtils.isNotNull(logTypeEnum)) {
//                categoryCode = logTypeEnum.getCategoryCode();
//            }
//        }
//        if (null == categoryCode) {
//            throw new BackendException("未获取到日志种类！");
//        }
//        //通过 根据日志种类编号和系统类型编号获取表配置信息
//        LogTableEnum logTableEnum = LogTableEnum.getLogTableEnum(categoryCode, platform);
//
//        return basicAllchipsLogDao.selectByPage(record, page, logTableEnum.getTableName(), categoryCode);
//    }
//
//    @AllchipsLog(module = LogModuleEnum.ACCOUNT, type = {LogTypeEnum.O_RESET_PASSWORD})
//    @Override
//    public void resetPwdLog(@AllchipsLogModel UserMessagePojo userMessagePojo, User oldUser, String str, boolean b) {
//        try {
//            OperationLogInfoDto operationLogInfoDto = new OperationLogInfoDto();
//            if (null != oldUser) {
//                operationLogInfoDto.setEntityId(Long.valueOf(oldUser.getUserId()));
//                operationLogInfoDto.setEntityName(oldUser.getNickName());
//            }
//            operationLogInfoDto.setOperationLogTemplate(OperationLogTemplateEnum.RESET_PASSWORD);
//            Map<String, Object> map = new HashMap<>();
//            map.put("result", str);
//            operationLogInfoDto.setDescParamMap(map);
//            userMessagePojo.setOperationLogInfo(operationLogInfoDto);
//            if (b) {
//                // 操作人信息
//                OperatorInfoDto operatorInfoDto = new OperatorInfoDto();
//                if (null != oldUser) {
//                    operatorInfoDto.setCreateUserId(oldUser.getUserId());
//                    operatorInfoDto.setCreateUserName(BackendUtil.getUserName(oldUser));
//                    operatorInfoDto.setCreateUserEmail(oldUser.getEmail());
//                }
//                userMessagePojo.setOperatorInfoDto(operatorInfoDto);
//            }
//        } catch (Exception e) {
//            logger.error("重置密码错误{}", e);
//        }
//    }

//    @AllchipsLog(module = LogModuleEnum.ACCOUNT, type = {LogTypeEnum.O_BATCH_EDIT})
//    public void updateUserByGroupLog(@AllchipsLogModel UserMessagePojo userMessagePojo, Page oldUser, StringBuilder deptName, StringBuilder deptId) {
//        try {
//
//            OperationLogInfoDto operationLogInfoDto = new OperationLogInfoDto();
//            operationLogInfoDto.setEntityId(Long.valueOf(oldUser.getUserId()));
//            operationLogInfoDto.setEntityName(oldUser.getNickName());
//            operationLogInfoDto.setOperationLogTemplate(OperationLogTemplateEnum.UPDATE_USER_BY_GROUP);
//            Map<String, Object> map = new HashMap<>();
//            map.put("userId", oldUser.getUserId());
//            map.put("userLoginName", oldUser.getUserName());
//            map.put("employeeName", oldUser.getNickName());
//            map.put("deptName", deptName.toString());
//            map.put("deptId", deptId.toString());
//            map.put("result", "成功");
//            operationLogInfoDto.setDescParamMap(map);
//            userMessagePojo.setOperationLogInfo(operationLogInfoDto);
//        } catch (Exception e) {
//            logger.error("登录日志错误{}", e);
//        }
//    }


//    @AllchipsLog(module = LogModuleEnum.ACCOUNT, type = {LogTypeEnum.O_CANCEL_RELEVANCE})
//    public void disRelevanceStatusLog(@AllchipsLogModel UserMessagePojo userMessagePojo, User loginUser, User userId) {
//        try {
//
//            OperationLogInfoDto operationLogInfoDto = new OperationLogInfoDto();
//            operationLogInfoDto.setEntityId(Long.valueOf(userId.getUserId()));
//            operationLogInfoDto.setEntityName(userId.getNickName());
//            operationLogInfoDto.setOperationLogTemplate(OperationLogTemplateEnum.DIS_RELEVANCE_STATUS);
//            Map<String, Object> map = new HashMap<>();
//            map.put("userId", loginUser.getUserId());
//            map.put("userLoginName", loginUser.getUserName());
//            map.put("employeeName", userId.getNickName());
//            map.put("id", userId.getUserId());
//            operationLogInfoDto.setDescParamMap(map);
//            userMessagePojo.setOperationLogInfo(operationLogInfoDto);
//        } catch (Exception e) {
//            logger.error("取消关联错误{}", e);
//        }
//    }

//    @AllchipsLog(module = LogModuleEnum.ROLE, type = {LogTypeEnum.O_BATCH_EDIT})
    public void editRoleGroupLog(@AllchipsLogModel BaseLogEntity baseLogEntity, User user, Integer id) {
        OperationLogInfoDto operationLogInfoDto = new OperationLogInfoDto();
        operationLogInfoDto.setEntityId(Long.valueOf(user.getUserId()));
        operationLogInfoDto.setEntityName(user.getNickName());
        operationLogInfoDto.setOperationLogTemplate(OperationLogTemplateEnum.EDIT_ROLE_GROUP);
        Map<String, Object> map = new HashMap<>();
        map.put("userId", user.getUserId());
        map.put("userLoginName", user.getUserName());
        map.put("employeeName", user.getNickName());
        map.put("roleId", CommUtils.toStr(id));
        map.put("result", "成功");
        operationLogInfoDto.setDescParamMap(map);
        baseLogEntity.setOperationLogInfo(operationLogInfoDto);
    }

    @AllchipsLog(module = LogModuleEnum.ACCOUNT, type = {LogTypeEnum.O_LOGIN})
    public void longinSetUserInfoLog(@AllchipsLogModel BaseLogEntity baseLogEntity, User user) {
        try {
            OperationLogInfoDto operationLogInfoDto = new OperationLogInfoDto();
            operationLogInfoDto.setEntityId(Long.valueOf(user.getUserId()));
            operationLogInfoDto.setEntityName(user.getNickName());

            operationLogInfoDto.setOperationLogTemplate(OperationLogTemplateEnum.LOGIN);
            Map<String, Object> map = new HashMap<>();
            map.put("userId", user.getUserId());
            map.put("userLoginName", user.getUserName());
            map.put("employeeName", user.getNickName());
            map.put("result", "成功");
            operationLogInfoDto.setDescParamMap(map);
            baseLogEntity.setOperationLogInfo(operationLogInfoDto);

            // 操作人信息
            OperatorInfoDto operatorInfoDto = new OperatorInfoDto();
            operatorInfoDto.setCreateUserId(user.getUserId());
//            operatorInfoDto.setCreateUserName(BackendUtil.getUserName(user));
            operatorInfoDto.setCreateUserEmail(user.getEmail());
            baseLogEntity.setOperatorInfoDto(operatorInfoDto);
        } catch (Exception e) {
            logger.error("登录错误{}", e);
        }
    }

    @AllchipsLog(module = LogModuleEnum.ACCOUNT, type = {LogTypeEnum.O_ENABLE})
    public void userOpen(@AllchipsLogModel BaseLogEntity baseLogEntity, User user) {
        try {
            OperationLogInfoDto operationLogInfoDto = new OperationLogInfoDto();
            operationLogInfoDto.setEntityId(Long.valueOf(user.getUserId()));
            operationLogInfoDto.setEntityName(user.getNickName());

            operationLogInfoDto.setOperationLogTemplate(OperationLogTemplateEnum.USER_OPEN);
            Map<String, Object> map = new HashMap<>();
            map.put("userId", user.getUserId());
            map.put("employeeName", user.getNickName());
            operationLogInfoDto.setDescParamMap(map);
            baseLogEntity.setOperationLogInfo(operationLogInfoDto);
        } catch (Exception e) {
            logger.error("启用错误{}", e);
        }
    }

    @AllchipsLog(module = LogModuleEnum.ACCOUNT, type = {LogTypeEnum.O_DISABLE})
    public void userClose(@AllchipsLogModel BaseLogEntity baseLogEntity, User userById) {
        try {
            OperationLogInfoDto operationLogInfoDto = new OperationLogInfoDto();
            operationLogInfoDto.setEntityId(Long.valueOf(userById.getUserId()));
            operationLogInfoDto.setEntityName(userById.getNickName());

            operationLogInfoDto.setOperationLogTemplate(OperationLogTemplateEnum.USER_CLOSE);
            Map<String, Object> map = new HashMap<>();
            map.put("userId", userById.getUserId());
            map.put("employeeName", userById.getNickName());
            operationLogInfoDto.setDescParamMap(map);
            baseLogEntity.setOperationLogInfo(operationLogInfoDto);
        } catch (Exception e) {
            logger.error("禁用错误{}", e);
        }
    }

//    @AllchipsLog(module = LogModuleEnum.ROLE, type = {LogTypeEnum.O_ENABLE})
//    public void roleLogEnable(@AllchipsLogModel BaseLogEntity baseLogEntity, Role role) {
//        try {
//            OperationLogInfoDto operationLogInfoDto = new OperationLogInfoDto();
//            operationLogInfoDto.setEntityId(Long.valueOf(role.getId()));
//            operationLogInfoDto.setEntityName(role.getRoleName());
//            operationLogInfoDto.setOperationLogTemplate(OperationLogTemplateEnum.ROLE_ENABLE);
//            Map<String, Object> map = new HashMap<>();
//            map.put("roleName", role.getRoleName());
//            map.put("roleId", role.getId());
//            operationLogInfoDto.setDescParamMap(map);
//            baseLogEntity.setOperationLogInfo(operationLogInfoDto);
//        } catch (Exception e) {
//            logger.error("角色启用错误{}", e);
//        }
//    }

//    @AllchipsLog(module = LogModuleEnum.DATA_PERMISSION, type = {LogTypeEnum.O_DISABLE})
//    public void dataPermissionLogDeactivate(@AllchipsLogModel BaseLogEntity baseLogEntity, DataPermission dataPermission) {
//        try {
//            OperationLogInfoDto operationLogInfoDto = new OperationLogInfoDto();
//            operationLogInfoDto.setEntityId(Long.valueOf(dataPermission.getId()));
//            operationLogInfoDto.setEntityName(dataPermission.getPermissionName());
//            operationLogInfoDto.setOperationLogTemplate(OperationLogTemplateEnum.PERMISSION_DEACTIVATE);
//            Map<String, Object> map = new HashMap<>();
//            map.put("permissionName", dataPermission.getPermissionName());
//            map.put("id", dataPermission.getId());
//            operationLogInfoDto.setDescParamMap(map);
//            baseLogEntity.setOperationLogInfo(operationLogInfoDto);
//        } catch (Exception e) {
//            logger.error("权限禁用错误{}", e);
//        }
//    }

//    @AllchipsLog(module = LogModuleEnum.DATA_PERMISSION, type = {LogTypeEnum.O_ENABLE})
//    @Override
//    public void dataPermissionLogEnable(@AllchipsLogModel BaseLogEntity baseLogEntity, DataPermission dataPermission) {
//        try {
//            OperationLogInfoDto operationLogInfoDto = new OperationLogInfoDto();
//            operationLogInfoDto.setEntityId(Long.valueOf(dataPermission.getId()));
//            operationLogInfoDto.setEntityName(dataPermission.getPermissionName());
//            operationLogInfoDto.setOperationLogTemplate(OperationLogTemplateEnum.PERMISSION_ENABLE);
//            Map<String, Object> map = new HashMap<>();
//            map.put("permissionName", dataPermission.getPermissionName());
//            map.put("id", dataPermission.getId());
//            operationLogInfoDto.setDescParamMap(map);
//            baseLogEntity.setOperationLogInfo(operationLogInfoDto);
//        } catch (Exception e) {
//            logger.error("权限启用错误{}", e);
//        }
//    }

//    @AllchipsLog(module = LogModuleEnum.ACCOUNT, type = {LogTypeEnum.O_EDIT_PASSWORD})
//    @Override
//    public void updatePwdLog(@AllchipsLogModel UserMessagePojo userMessagePojo, User dbUser, String str) {
//        try {
//            OperationLogInfoDto operationLogInfoDto = new OperationLogInfoDto();
//            if (null != dbUser) {
//                operationLogInfoDto.setEntityId(Long.valueOf(dbUser.getUserId()));
//                operationLogInfoDto.setEntityName(dbUser.getNickName());
//            }
//            operationLogInfoDto.setOperationLogTemplate(OperationLogTemplateEnum.UPDATE_PASSWORD);
//            Map<String, Object> map = new HashMap<>();
//            map.put("result", str);
//            operationLogInfoDto.setDescParamMap(map);
//            userMessagePojo.setOperationLogInfo(operationLogInfoDto);
//        } catch (Exception e) {
//            logger.error("修改密码错误{}", e);
//        }
//    }

//    @AllchipsLog(module = LogModuleEnum.ROLE, type = {LogTypeEnum.O_DISABLE})
//    @Override
//    public void roleLogDeactivate(@AllchipsLogModel BaseLogEntity baseLogEntity, Role role) {
//        try {
//            OperationLogInfoDto operationLogInfoDto = new OperationLogInfoDto();
//            operationLogInfoDto.setEntityId(Long.valueOf(role.getId()));
//            operationLogInfoDto.setEntityName(role.getRoleName());
//            operationLogInfoDto.setOperationLogTemplate(OperationLogTemplateEnum.ROLE_DEACTIVATE);
//            Map<String, Object> map = new HashMap<>();
//            map.put("roleName", role.getRoleName());
//            map.put("roleId", role.getId());
//            operationLogInfoDto.setDescParamMap(map);
//            baseLogEntity.setOperationLogInfo(operationLogInfoDto);
//        } catch (Exception e) {
//            logger.error("角色禁用错误{}", e);
//        }
//    }

//    private void setEntity(UserMessagePojo userMessagePojo, User oldUser, OperationLogTemplateEnum updateuserbygroup) {
//        OperationLogInfoDto operationLogInfoDto = new OperationLogInfoDto();
//        operationLogInfoDto.setEntityId(Long.valueOf(oldUser.getUserId()));
//        operationLogInfoDto.setEntityName(oldUser.getNickName());
//        operationLogInfoDto.setOperationLogTemplate(updateuserbygroup);
//        Map<String, Object> map = new HashMap<>();
//        map.put("userId", oldUser.getUserId());
//        map.put("userLoginName", oldUser.getUserName());
//        map.put("employeeName", oldUser.getNickName());
//        map.put("result", "成功");
//        operationLogInfoDto.setDescParamMap(map);
//        userMessagePojo.setOperationLogInfo(operationLogInfoDto);
//    }
}
